package com.worldtolearn.user.bean;

import java.util.Calendar;
import java.util.Date;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;


//import com.worldtolearn.user.bu.RegisterBu;
import com.worldtolearn.user.bu.UserBu;
import com.worldtolearn.user.entity.User;

@Controller
@ManagedBean
@SessionScoped
public class CreateAccountMB {

	// TODO A REFAIRE COMPLETEMENT / RENOMMER
	
	@Inject
	private User user;
	
	private int clientDateOfBirthDay = 0;
	private int clientDateOfBirthMonth  = 0;
	private int clientDateOfBirthYear  = 0;
	
	
	@Inject
	UserBu userBu;
//	RegisterBu registerBu;
	@Inject
	LoginMB loginMb;
	
	
	public CreateAccountMB() {

	}
	
	public User getUser() {
		return user;
	}
	public int getClientDateOfBirthDay() {
		return clientDateOfBirthDay;
	}
	public int getClientDateOfBirthMonth() {
		return clientDateOfBirthMonth;
	}
	public int getClientDateOfBirthYear() {
		return clientDateOfBirthYear;
	}

	public void setUser(User user) {
		this.user = user;
	}
	public void setClientDateOfBirthDay(int clientDateOfBirthDay) {
		this.clientDateOfBirthDay = clientDateOfBirthDay;
	}
	public void setClientDateOfBirthMonth(int clientDateOfBirthMonth) {
		this.clientDateOfBirthMonth = clientDateOfBirthMonth;
	}
	public void setClientDateOfBirthYear(int clientDateOfBirthYear) {
		this.clientDateOfBirthYear = clientDateOfBirthYear;
	}


	public UserBu getUserBu() {
		return userBu;
	}

	public void setUserBu(UserBu userBu) {
		this.userBu = userBu;
	}
	
	
	
	public String createUser() {

		/*
		 * TODO SOUMISSION DU COMPTE
		 * QUELLE REDIRECTION ?
		 */

		
		// *** Créer un rôle en dur ***
		user.setRole("Client");

		// *** Créer une date conforme ***
		Calendar calendarClientBirthDate = Calendar.getInstance();
		calendarClientBirthDate.set(Calendar.MONTH, (clientDateOfBirthMonth - 1));
		calendarClientBirthDate.set(Calendar.YEAR, clientDateOfBirthYear);
		calendarClientBirthDate.set(Calendar.DAY_OF_MONTH, clientDateOfBirthDay);
		Date clientBirthDate = calendarClientBirthDate.getTime();
		user.setDateOfBirth(clientBirthDate);
		
		// TODO *** A verifier que la date donnée corresponde toujours ***
		System.out.println("******************************************************************");
		System.out.println("CreateAccountMB - createUser() | user : " + user);
		System.out.println("******************************************************************");

		
		// *** transferer un USER avec un ROLE EN DUR !!! ***
		userBu.create(user);
		HttpServletRequest request = (HttpServletRequest) FacesContext
				.getCurrentInstance().getExternalContext().getRequest();
		HttpSession session = ((HttpServletRequest) request).getSession(false);
		session.setAttribute("user", user);
//		loginMb.submit();
		userBu.connect(user.getMail(), user.getPassword());
		session.setAttribute("loginMB", loginMb);
		
		System.out.println("*** Retour dans CreateAccountMB - user cree et connecté ***"+ userBu.isConnected());
		System.out.println("*** Retour dans CreateAccountMB - createUser() ***");
		
		return "offerslist";
	}
	

	
	public LoginMB getLoginMb() {
		return loginMb;
	}
	public void setLoginMb(LoginMB loginMb) {
		this.loginMb = loginMb;
	}
	
	public String updateUser() {
		
		// *** Créer un rôle en dur ***
		user.setRole("Client");

		// *** Créer une date conforme ***
		Calendar calendarClientBirthDate = Calendar.getInstance();
		calendarClientBirthDate.set(Calendar.MONTH, (clientDateOfBirthMonth - 1));
		calendarClientBirthDate.set(Calendar.YEAR, clientDateOfBirthYear);
		calendarClientBirthDate.set(Calendar.DAY_OF_MONTH, clientDateOfBirthDay);
		Date clientBirthDate = calendarClientBirthDate.getTime();
		user.setDateOfBirth(clientBirthDate);
		
		// TODO *** A verifier que la date donnée corresponde toujours ***
		System.out.println("******************************************************************");
		System.out.println("CreateAccountMB - updateUser() | user : " + user);
		System.out.println("******************************************************************");
		
		userBu.update(user);
		loginMb.submit();
		
		System.out.println("*** Retour dans CreateAccountMB - createUser() ***");
		
		return "offerslist";
	}
	
}
